<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车间任务编码" prop="wkspCode">
        <el-input v-model="queryParams.wkspCode" placeholder="请输入车间任务编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="领料编码" prop="pickCode">
        <el-input v-model="queryParams.pickCode" placeholder="请输入领料编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['mes:revokepick:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['mes:revokepick:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['mes:revokepick:remove']">删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['mes:revokepick:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="revokepickList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="${comment}" align="center" prop="id" /> -->
      <!-- <el-table-column label="车间任务ID" align="center" prop="wkspTaskId" /> -->
      <el-table-column label="车间任务编码" align="center" prop="wkspCode" />
      <el-table-column label="领料编码" align="center" prop="pickCode" />
      <el-table-column label="物料编码" align="center" prop="mtrCode" />
      <el-table-column label="物料名称" align="center" prop="mtrName" />
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <el-tag :type="tagType(scope.row.status)" effect="dark">
            <dict-tag :options="dict.type.mes_inhou_status" :value="scope.row.status" />
          </el-tag>
          
        </template>
      </el-table-column>
      <el-table-column label="撤回数量" align="center" prop="revokeQuantity" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status==0" size="mini" type="text" icon="el-icon-edit" @click="ruku(scope.row)"
            v-hasPermi="['mes:revokepick:edit']">入库</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:revokepick:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['mes:revokepick:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />


      <transition name="el-zoom-in-center">
      <el-dialog id="rukudialog" title="选择仓库" :visible.sync="rukuopen" v-if="rukuopen" width="500px" append-to-body>
      <label>入库仓库：</label>
      <el-select v-model="houId" filterable placeholder="请选择" @change="chooseHou">
        <el-option v-for="item in houoptions.filter(item => { return item.houType == 0 })" :key="item.houId"
          :label="item.houName" :value="item.houId">
        </el-option>
      </el-select>
      <el-select v-if="houId" v-model="locatorId" filterable placeholder="请选择" style="width: 30%;margin-left: 20px;">
          <el-option v-for="item in locator" :key="item"
            :label="item.code" :value="item.id">
          </el-option>
        </el-select>

      <transition name="el-zoom-in-top">
        <div v-show="houId" v-if="houId">
          <el-card id="rukucard" style="margin-top: 10px;border-radius: 15px;background-color: rgba(132, 200, 205, 0.557);"
            shadow="hover">
            <label style="display: inline-block;margin: auto;margin-top: 10px;">库存总容量: <span>{{
              houoptions[houoptions.findIndex(x => x.houId === houId)].houCapacity - houoptions[houoptions.findIndex(x
                =>
                x.houId
                === houId)].houRemainCapacity }} / {{ houoptions[houoptions.findIndex(x => x.houId === houId)].houCapacity
    }}</span></label>
            <el-progress :text-inside="true" :color="customColorMethod(calculateInventory())" :stroke-width="20"
              :percentage="calculateInventory()" status="exception"></el-progress>
          </el-card>

        </div>

      </transition>
      <el-divider content-position="left">入库产品</el-divider>
      <el-card id="rukucard" style=" margin-top: 10px;border-radius: 15px;background-color: rgba(75, 193, 214, 0.557);"
        shadow="always">
        <div slot="header" class="clearfix">
          <h5 style="display: inline-block;">物料编码: <h3 style="display: inline-block;font-style:italic;">{{
            nowrowData.mtrCode }}</h3>
          </h5>
        </div>
        <div style="display: inline-block;">
          <h2 style="display: block;font-weight: 700;">物料名称: {{ nowrowData.mtrName }}</h2>
          <h3 style="display: block;color:#565555">入库数量: {{ nowrowData.revokeQuantity }}</h3>
          <!-- <h3 style="display: block;color:#565555">供应商: {{ nowrowData.supplier }}</h3> -->
        </div>

        <img style="display: inline-block;float: right;" src="@/assets/images/mtr.png" />
      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="inHou">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </transition>


      <!-- <el-dialog title="选择仓库" :visible.sync="rukuopen" v-if="rukuopen" width="500px" append-to-body>
      <label>入库仓库：</label>
      <el-select v-model="houId" filterable placeholder="请选择">
        <el-option v-for="item in houoptions.filter(item => { return item.houType == 0 })" :key="item.houId"
          :label="item.houName" :value="item.houId">
        </el-option>
      </el-select>
      <transition name="el-zoom-in-top">
        <div v-show="houId" v-if="houId">
          <el-card  shadow="hover" >
            <label style="display: block;margin-top: 10px;">库存总容量: <span>{{ houoptions[houoptions.findIndex(x => x.houId
              ===
              houId)].houCapacity }}</span></label>


          </el-card>
          <el-card  shadow="hover" >
            <label style="display: block;margin-top: 10px;">库存剩余容量: <span>{{
              houoptions[houoptions.findIndex(x => x.houId
                === houId)].houRemainCapacity }}</span></label>
          </el-card>
        </div>

      </transition>
      <el-divider content-position="left">入库产品</el-divider>
      <el-card shadow="hover">
        <label style="display: block;background-color: rgba(249, 192, 106, 0.637);">物料编码:{{ nowrowData.mtrCode }}</label>
        <label style="display: block;background-color: rgba(186, 250, 255, 0.637);">物料名称:{{ nowrowData.mtrName }}</label>
        <label style="display: block;">入库数量:{{ nowrowData.revokeQuantity }}</label>
        <label style="display: block;">供应商:{{ nowrowData.supplier }}</label>
      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="inHou">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->




    <!-- 添加或修改领料返回对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="车间任务ID" prop="wkspTaskId">
          <el-input v-model="form.wkspTaskId" placeholder="请输入车间任务ID" />
        </el-form-item>
        <el-form-item label="车间任务编码" prop="wkspCode">
          <el-input v-model="form.wkspCode" placeholder="请输入车间任务编码" />
        </el-form-item>
        <el-form-item label="领料编码" prop="pickCode">
          <el-input v-model="form.pickCode" placeholder="请输入领料编码" />
        </el-form-item>
        <el-form-item label="物料编码" prop="mtrCode">
          <el-input v-model="form.mtrCode" placeholder="请输入物料编码" />
        </el-form-item>
        <el-form-item label="物料名称" prop="mtrName">
          <el-input v-model="form.mtrName" placeholder="请输入物料名称" />
        </el-form-item>
        <el-form-item label="撤回数量" prop="revokeQuantity">
          <el-input v-model="form.revokeQuantity" placeholder="请输入撤回数量" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRevokepick, getRevokepick, delRevokepick, addRevokepick, updateRevokepick,inHou} from "@/api/mes/revokepick";
import { listHouinfo } from "@/api/mes/houinfo"
import { getLocatorByHouId } from "@/api/mes/locator"
export default {
  name: "Revokepick",
  dicts: ['mes_inhou_status'],
  data() {
    return {
      data:{},
      //选择的库位id
      locatorId:"",
      locator:[],//库位下拉数据
      //选择的仓库
      houId: "",
      //库房下拉框数据
      houoptions:[],
      //当前点击记录
      nowrowData:[],
      //入库对话框
      rukuopen:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 领料返回表格数据
      revokepickList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        wkspCode: null,
        pickCode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    chooseHou(e){
      getLocatorByHouId({houId:e}).then(res=>{
        console.log(res)
        this.locator=res.data
      })
    },
    //标签类型判断
    tagType(status) {
      switch (status) {
        case 0:
          return ""
        case 1:
          return "success"
      }
    },
     //计算库存比例
     calculateInventory() {
      return parseFloat((1 - this.houoptions[this.houoptions.findIndex(x => x.houId
        === this.houId)].houRemainCapacity / this.houoptions[this.houoptions.findIndex(x => x.houId === this.houId)].houCapacity)* 100).toFixed(2)
    },

    //进度条颜色控制
    customColorMethod(data) {
      if (data < 30) {
        return '#f56c6c'
      } else if (data < 100) {
        return '#e6a23c';
      } else {
        return '#67c23a';
      }
    },
    //入库确认TODO--数据库未添加状态字段
    inHou(){
      // inHou({id:this.nowrowData.id,houId:this.houId}).then(res => {
      //   this.rukuopen = false
      //   this.$modal.msgSuccess("入库成功！");
      //   this.getList()
      // })

      inHou({ id: this.nowrowData.id, houId: this.houId,locatorInfoId:this.locatorId}).then(res => {
          this.rukuopen = false
          this.$modal.msgSuccess("入库成功！");
          this.getList()
      })
    },
    //入库按钮
    ruku(row){
      this.houId="",
      this.nowrowData = row
      listHouinfo().then(res => {
        this.houoptions = res.rows
        this.rukuopen = true
      })
    },
    /** 查询领料返回列表 */
    getList() {
      this.loading = true;
      listRevokepick(this.queryParams).then(response => {
        this.revokepickList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        wkspTaskId: null,
        wkspCode: null,
        pickCode: null,
        mtrCode: null,
        mtrName: null,
        revokeQuantity: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加领料返回";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRevokepick(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改领料返回";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRevokepick(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRevokepick(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除领料返回编号为"' + ids + '"的数据项？').then(function () {
        return delRevokepick(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/revokepick/export', {
        ...this.queryParams
      }, `revokepick_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
#rukucard>>>.el-card__header {
  background-color: rgb(214, 210, 254);
  padding-bottom: 0px;
}
#rukucard{
  box-shadow: 3px 3px 5px rgba(128, 128, 128, 0.699);
}
#rukudialog>>>.el-dialog{
border-radius: 10px;
}
</style>